package liuliu.throughwaring.ui.activity;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.net.Uri;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.main.IMainView;
import liuliu.throughwaring.control.main.MainListener;
import liuliu.throughwaring.control.net.HttpTask;
import liuliu.throughwaring.control.task.QueryTask;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.InvokeReturn;
import liuliu.throughwaring.model.NationAlarmModel;
import liuliu.throughwaring.model.NoticeModel;
import liuliu.throughwaring.model.PoliceModel;
import liuliu.throughwaring.service.NewCompany;
import liuliu.throughwaring.service.NoticeHandlerService;

/**
 * Created by liuliu on 2015/11/12   16:47
 * 主页面
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class MainActivity extends BaseActivity implements IMainView {
    public static MainActivity mInstance;
    @CodeNote(id = R.id.main_user_img, click = "onClick")
    ImageView main_user_img;
    @CodeNote(id = R.id.main_popedom_tv)
    TextView popedom_txt;//所属派出所
    @CodeNote(id = R.id.main_user_name_tv)
    TextView user_name_txt;//所属派出所
    @CodeNote(id = R.id.main_item_person_add_ll, click = "onClick")
    LinearLayout person_add;//人员添加
    @CodeNote(id = R.id.main_item_police_add_ll, click = "onClick")
    LinearLayout police_add;//民警添加
    @CodeNote(id = R.id.main_item_unit_add_ll, click = "onClick")
    LinearLayout unit_add;//单位添加
    @CodeNote(id = R.id.main_item_person_search_ll, click = "onClick")
    LinearLayout person_search;//人员查询
    @CodeNote(id = R.id.main_item_police_search_ll, click = "onClick")
    LinearLayout police_search;//民警查询
    @CodeNote(id = R.id.main_item_unit_search_ll, click = "onClick")
    LinearLayout unit_search;//单位查询
    @CodeNote(id = R.id.main_item_online_search_ll, click = "onClick")
    LinearLayout online_search;//联网查询
    @CodeNote(id = R.id.main_item_data_synchronization_ll, click = "onClick")
    LinearLayout data_synchronization;//数据同步
    @CodeNote(id = R.id.main_item_setting_ll, click = "onClick")
    LinearLayout setting;//系统设置
    @CodeNote(id = R.id.main_message_rl, click = "onClick")
    RelativeLayout main_message_rl;
    @CodeNote(id = R.id.main_message_show)
    View message_show;
    LLDialog llDialog;
    Dialog dialog;
    String[] persontype;
    MainListener mListener;
    PoliceModel policeModel;
    private NoticeHandlerService service;
   // private NoticeHandlerService.MyNoticeBinder binder = null;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(MainActivity.this, NoticeHandlerService.class);
       // bindService(intent, conn, Context.BIND_AUTO_CREATE);
        mInstance = this;
        persontype = new String[]{};
        mListener = new MainListener(this, this);
        policeModel = new PoliceModel();
        Intent newIntent = new Intent(MainActivity.this, NewCompany.class);
        startService(newIntent);
    }

//    ServiceConnection conn = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            binder = (NoticeHandlerService.MyNoticeBinder) service;
//            if (binder != null) {
//                binder.getNoticeBinder(new NoticeHandlerService.GetNoticeFace() {
//
//                    @Override
//                    public void FaceShow(boolean isNew) {
//                        if (isNew) {
//                            message_show.setVisibility(View.VISIBLE);
//                        } else {
//                            message_show.setVisibility(View.GONE);
//                        }
//                    }
//                });
//            }
//            ///service = ((NoticeHandlerService.MyNoticeBinder) service).getNoticeBinder(MainActivity.this);
//        }

//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//            service = null;
//        }
//    };

    @Override
    public void initEvents() {
        //加载民警信息
        String policeID = Utils.ReadString("PoliceId");
        List<PoliceModel> list = finalDb.findAllByWhere(PoliceModel.class, "PoliceId='" + policeID + "'");
        if (list.size() > 0) {
            policeModel = list.get(0);
            popedom_txt.setText(policeModel.getPoliceName());
        }
        main_user_img.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + 110));
                startActivity(intent);
                return false;
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       // unbindService(conn);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_item_person_add_ll:
                mUtils.IntentPost(AddWorkActivity.class);
                break;
            case R.id.main_item_police_add_ll:
                mUtils.IntentPost(RegPoliceActivity.class);
                break;
            case R.id.main_item_unit_add_ll:
                mUtils.IntentPost(RegUnitActivity.class);
                break;
            case R.id.main_item_person_search_ll:
                mUtils.IntentPost(PersonSearchActivity.class);
                break;
            case R.id.main_item_police_search_ll:
                mUtils.IntentPost(PoliceSearchActivity.class);
                break;
            case R.id.main_item_unit_search_ll:
                mUtils.IntentPost(UnitSearchActivity.class);
                break;
            case R.id.main_item_online_search_ll:
                mUtils.IntentPost(OnLineSearchActivity.class);
                break;
            case R.id.main_item_data_synchronization_ll:
                //下载更新数据
                dialog = Utils.ProgressDialog(MainActivity.this, dialog, "下载中，请稍候...", false);
                dialog.show();
                //mListener.refresh();
                break;
            case R.id.main_item_setting_ll:
                mUtils.IntentPost(SysSettingActivity.class);
                break;
            case R.id.main_user_img:
//                HttpTask task = new HttpTask(this);
//                task.start("GetNationAlarm", "", "NationAlarmModel", new HttpTask.TaskListener() {
//                    @Override
//                    public void onTaskResult(boolean result, InvokeReturn invokeReturn) {
//                        List data = invokeReturn.getData();
//                        List nation = finalDb.findAll(NationAlarmModel.class);
//                        if (result) {
//                            if (nation.size() > 0) {
//                                finalDb.deleteAll(NationAlarmModel.class);//删除所有离线数据
//                            }
//                            if (data.size() > 0) {
//                                for (Object s : data) {
//                                    finalDb.save(new NationAlarmModel(s.toString()));
//                                }
//                            }
//                        }
//                    }
//                });
                break;
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                exitTime = System.currentTimeMillis();
                //弹出是否退出系统
                llDialog = new LLDialog(this);
                llDialog.setMiddleMessage("确认要退出系统？");
                llDialog.setOnPositiveListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MainActivity.this.finish();
                        System.exit(0);
                    }
                });
                llDialog.show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    public void FaceShow(boolean isNew) {
//        if (isNew) {
//            message_show.setVisibility(View.VISIBLE);
//        } else {
//            message_show.setVisibility(View.GONE);
//        }
//    }

    @Override
    public void onMainRefreshResult(boolean isTrue, List<PoliceModel> list) {

    }
}
